package com.wzh.annotation;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

/**
 * @description: 注解
 * @author: WZH
 * @time: 2021/5/29 16:28
 */

public class AnnotationTest {
    public static void main(String[] args) throws Exception {
        // 获取类上的注解
        Class<Demo> clazz = Demo.class;
        MyAnnotation annotationOnClass = clazz.getAnnotation(MyAnnotation.class);
        System.out.println(annotationOnClass.getValue());



        // 获取成员变量上的注解
        Field name = clazz.getField("name");
        MyAnnotation annotationOnField = name.getAnnotation(MyAnnotation.class);
        System.out.println(annotationOnField.getValue());

        // 获取hello方法上的注解
        Method hello = clazz.getMethod("hello", (Class<?>[]) null);
        MyAnnotation annotationOnMethod = hello.getAnnotation(MyAnnotation.class);
        System.out.println(annotationOnMethod.getValue());

        // 获取defaultMethod方法上的注解
        Method defaultMethod = clazz.getMethod("defaultMethod", (Class<?>[]) null);
        MyAnnotation annotationOnDefaultMethod = defaultMethod.getAnnotation(MyAnnotation.class);
        System.out.println(annotationOnDefaultMethod.getValue());


        /**
         * 注解的保留策略  导致空指针
         *
         *
         *  Exception in thread "main" java.lang.NullPointerException
         * 	at com.wzh.annotation.AnnotationTest.main(AnnotationTest.java:17)
         *
         * 	 @Target  限定注解位置
         *   @Retention（注解的保留策略）
         *
         */
    }
}